
// Author: Zemian Deng
// Date: 03/15/2010
//
// Generate a pom.xml file that contains all jboss jar files as maven
// dependencies (using systemPath). Note that this is not a good practice
// and should only be used if no other way to get jboss jars resolve through
// other means.
// 
// See README.txt for more information.

new Main().run(args)
class Main {
	def jbossHome
	def jbossVersion
	def run(args) {
		if (args.length < 1) {
			System.err.prinltn("Usage: groovy generatePomXml <JBOSS_HOME>")
		}
		jbossHome = args[0]
		jbossVersion = args[1]
		def pomToGenerate = [
			["client", ["client"]],
			["server-lib", ["lib"]],
			["server-common-lib", ["common/lib"]],
			["server-all-lib", ["server/all/lib"]]
		]
		pomToGenerate.each { input ->
			def pomFile = new File("${input[0]}-${jbossVersion}.xml")
			pomFile.withPrintWriter { out -> 
				out.println(('''
					<?xml version="1.0" encoding="UTF-8"?>
					<!--
					Author: Zemian Deng
					Date: ''' + new Date() + '''
					-->
					<project 
						xmlns="http://maven.apache.org/POM/4.0.0" 
						xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
						xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">					
					<modelVersion>4.0.0</modelVersion>
					<groupId>deng.jboss.jboss-local-lib</groupId>
					<artifactId>''' + input[0] + '''</artifactId>
					<version>''' + jbossVersion + '''</version>
					<packaging>pom</packaging>
					<properties>
						<!-- JBoss Properties -->
						<jboss.home>''' + jbossHome.replace("\\", "/") + '''</jboss.home>
						<jboss.version>''' + jbossVersion + '''</jboss.version>
					</properties>
					<dependencies>
					<!-- JBoss Local Lib With System Path -->
				''').replaceAll("(?m)^\\s*", ""))
				printSystemPathDependency(out, input[1])
				out.println('''</dependencies>\n</project>''')
			} // end print writer.
			println("Generated $pomFile")
		}
	}

	def printSystemPathDependency(out, jarDirs) {
		out.flush()
		jarDirs.each { name ->
			def dir = new File("${jbossHome}/${name}")
			dir.eachFile{ jar ->
				if (jar.isFile() && jar =~ /\.jar$/) {
					def groupdId = name.replace("/", ".")
					def artifactId = jar.name - ".jar"
					out.println("""
						<dependency>
							<groupId>deng.jboss-local-lib.${groupdId}</groupId>
							<artifactId >${artifactId}</artifactId>
							<version>\${jboss.version}</version>
							<scope>system</scope>
							<systemPath>\${jboss.home}/${name}/${jar.name}</systemPath>
						</dependency>
					""".replaceAll("(?m)^\\s*", ""))
				}
			}
		}
		out.flush()
	}
}